package com.mmall.service;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;
import com.mmall.dao.SysDeptMapper;
import com.mmall.dto.DeptLeveDto;
import com.mmall.model.SysDept;
import com.mmall.util.LevelUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MultiMap;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Created by pc on 2019/3/29.
 */
@Service
public class SysTreeService {
    @Resource
    private SysDeptMapper sysDeptMapper;

    public List<DeptLeveDto> deptTree(){
        List<SysDept> deptList = sysDeptMapper.getAllDept();
        List<DeptLeveDto> dtoList = Lists.newArrayList();
        for(SysDept dept:deptList){
           DeptLeveDto dto =  DeptLeveDto.adpt(dept);
           dtoList.add(dto);
        }
        return dtoListToTree(dtoList);
    }

    public List<DeptLeveDto> dtoListToTree(List<DeptLeveDto> dtoList){
        if(CollectionUtils.isEmpty(dtoList)){
                return Lists.newArrayList();
        }
        //拿出父级菜单   Multimap key值相同时 不会覆盖 会组成集合
        Multimap<String,DeptLeveDto> leveDtoMultiMap = ArrayListMultimap.create();
       List<DeptLeveDto> rootList = Lists.newArrayList();
       for(DeptLeveDto dto:dtoList){
           leveDtoMultiMap.put(dto.getLevel(),dto);
           if(LevelUtil.ROOT.equals(dto.getLevel())){
               rootList.add(dto);
           }
       }

       //按照seq从小到大排序
        Collections.sort(rootList, new Comparator<DeptLeveDto>() {
            @Override
            public int compare(DeptLeveDto o1, DeptLeveDto o2) {
                return o1.getSeq() - o2.getSeq();
            }
        });

        //递归生成树
        transformDeptTree(rootList,LevelUtil.ROOT,leveDtoMultiMap);
        return rootList;
    }


    //递归排序
    public void transformDeptTree(List<DeptLeveDto> deptLeveList,String leve,Multimap<String,DeptLeveDto> deptMap){
        for(int i=0;i<deptLeveList.size();i++){
            //遍历该层的每一个元素
            DeptLeveDto deptLeveDto = deptLeveList.get(i);
            //获取当前层级的leve
            String nextLevel = LevelUtil.calculateLevel(leve,deptLeveDto.getId());
            //处理下一层  可以得到多个相同级别的dto 得到key相同的部门 同时放入下一层
            List<DeptLeveDto> tempDeptList = (List<DeptLeveDto>) deptMap.get(nextLevel);
            if(CollectionUtils.isNotEmpty(tempDeptList)){
                //排序
                Collections.sort(tempDeptList,deptSeqCompartor);
                //设置下一层部门
                deptLeveDto.setDeptList(tempDeptList);
                //进入到下一层处理
                transformDeptTree(tempDeptList,nextLevel,deptMap);
            }
        }
    }

    public Comparator<DeptLeveDto> deptSeqCompartor = new Comparator<DeptLeveDto>() {
        @Override
        public int compare(DeptLeveDto o1, DeptLeveDto o2) {
            return o1.getSeq() - o2.getSeq();
        }
    };
}
